// -----------------------------------------------------
//  controls
// -----------------------------------------------------
// 0 - label
// 1 - textbox
// 2 - checkbox
// 3 - combo
// 4 - selector
// 5 - datepicker
// 6 - checklist

var ItemsCommon =
{
    getItemValue: function(itemType, controlId)
    {
        switch(itemType) {
            // view item (span)
            case 0:
                var control = document.getElementById(controlId);            
                return control.innerHTML;
            
            // textbox
            case 1:
                var control = document.getElementById(controlId);            
                return control.value;
            
            // checkbox
            case 2:
                var control = document.getElementById(controlId);            
                return control.checked;        
                
            // combo
            case 3:
                var combo = ComboList_Get(controlId);
                return combo.GetValueList();
                
            // selector
            case 4:
                var selector = Selector_Get(controlId);
                return selector.getValue();        
                
            // datepicker
            case 5:
                var datepicker = DatePicker_Get(controlId);
                return datepicker.getValue();      
                
            // checklist
            case 6:
                var checklist = CheckList_Get(controlId);
                return checklist.GetValueList();        
                
            // datepickercombos
            case 7:
                var datepicker = DatePickerCombos_Get(controlId);
                return datepicker.getValue();                  
        }        
    },

    setItemValue: function(itemType, controlId, value)
    {
        switch(itemType) {
            // view item (spam)
            case 0:
                var control = document.getElementById(controlId);
                control.innerHTML = value;
                break;
            
            // textbox
            case 1:
                var control = document.getElementById(controlId);
                control.value = value;
                break;            
            
            // checkbox
            case 2:
                var control = document.getElementById(controlId);
                control.checked = value;
                break;
                                    
            // combo
            case 3:
                var combo = ComboList_Get(controlId);
                combo.SelectItem(value);
                break;
                            
            // selector
            case 4:
                var selector = Selector_Get(controlId);
                selector.setValue(value);        
                break;            
                
            // datepicker
            case 5:
                var datepicker = DatePicker_Get(controlId);
                
                if(typeof(value) == "string")
                    datepicker.setValue(value);        
                else
                    datepicker.setDateValue(value);     
                       
                break;  
                
            // checklist
            case 6:
                var checklist = CheckList_Get(controlId);
                checklist.SetValueList(value);     
                break;   
                
            // datepickercombos
            case 7:
                var datepicker = DatePickerCombos_Get(controlId);
                return datepicker.setValue(value);                                                           
        }        
    },

    getItemDescription: function(itemType, controlId)
    {
        switch(itemType) {
            // combo
            case 3:
                var combo = ComboList_Get(controlId);
                return combo.GetCaptionList();
                
            // selector
            case 4:
                var selector = Selector_Get(controlId);
                return selector.getDescription();        
                
            default:
                return this.getItemValue(itemType, controlId);
        }        
    },
    
   setItemDescription: function(itemType, controlId, value)
    {
        switch(itemType) {
            // selector
            case 4:
                var selector = Selector_Get(controlId);
                selector.setDescription(value);        
                break;    
        }        
    },
        
    getItemEntityName: function(itemType, controlId)
    {
        switch(itemType) {
            // combo
            case 3:
                var combo = ComboList_Get(controlId);
                if(combo.entityName)
                    return combo.entityName;
                else
                    return '';
                break;
                        
            // selector
            case 4:
                var selector = Selector_Get(controlId);
                return selector.entityName;        

            // checklist
            case 6:
                var checklist = CheckList_Get(controlId);
                return checklist.entityName;        

            default:
                return '';                                 
        }        
    },
            
    cleanValue: function(itemType, controlId)
    {
        switch(itemType) {
            // view item (span)
            case 0:
                var control = document.getElementById(controlId);            
                control.innerHTML = "";
                break;
                
            // textbox
            case 1:
                var control = document.getElementById(controlId);            
                control.value = "";
                break;
                
            // checkbox
            case 2:
                var control = document.getElementById(controlId);            
                control.checked = false;        
                break;
                
            // combo
            case 3:
                var combo = ComboList_Get(controlId);
                combo.Unselect();
                break;                
                
            // selector
            case 4:
                var selector = Selector_Get(controlId);
                selector.setValue("");        
                selector.setDescription("");        
                break;
                
            // datepicker
            case 5:
                var datepicker = DatePicker_Get(controlId);
                datepicker.clearValue();      
                break;                
                
            // checklist
            case 6:
                var checklist = CheckList_Get(controlId);
                checklist.ClearSelection();        
                break;                
                
            // datepickercombos
            case 7:
                var datepicker = DatePickerCombos_Get(controlId);
                datepicker.cleanValue();                  
                break;                
        }        
    },
            
    enableItem : function(itemType, controlId)
    {
        switch(itemType) {
            // view item (span)
            case 0:
                break;
            
            // textbox
            case 1:
                var control = document.getElementById(controlId);
                control.readOnly = false;
                control.className = (control.className == "textBoxDisabled") ? "textBox" : "textArea";
                break;            
            
            // checkbox
            case 2:
                var control = document.getElementById(controlId);            
                control.disabled = false;
                
                if(control.parentElement)
                    control.parentElement.disabled = false;
                else
                    control.parentNode.disabled = false;

                break;
                                    
            // combo
            case 3:
                var combo = ComboList_Get(controlId);
                combo.Enable();
                break;
                            
            // selector
            case 4:
                var selector = Selector_Get(controlId);
                selector.enable();        
                break;            
                
            // datepicker
            case 5:
                var datePicker = DatePicker_Get(controlId);
                datePicker.enable();        
                break; 
                      
            // checklist
            case 6:
                var checklist = CheckList_Get(controlId);
                checklist.Enable();        
                break; 
                
            // datepickercombos
            case 7:
                var datepicker = DatePickerCombos_Get(controlId);
                return datepicker.enable();                                          
        }        
    },

    disableItem: function(itemType, controlId)
    {
        switch(itemType) {
            // view item (spam)
            case 0:
                break;
            
            // textbox
            case 1:
                var control = document.getElementById(controlId);            
                control.readOnly = true;
                control.className = (control.className == "textBox") ? "textBoxDisabled" : "textAreaDisabled";
                break;            
            
            // checkbox
            case 2:
                var control = document.getElementById(controlId);            
                control.disabled = true;

                if(control.parentElement)
                    control.parentElement.disabled = true;
                else
                    control.parentNode.disabled = true;
                    
                break;
                                    
            // combo
            case 3:
                var combo = ComboList_Get(controlId);
                combo.Disable();
                break;
                            
            // selector
            case 4:
                var selector = Selector_Get(controlId);
                selector.disable();        
                break;   
                
            // datepicker
            case 5:
                var datePicker = DatePicker_Get(controlId);
                datePicker.disable();        
                break;      
                
            // checklist
            case 6:
                var checklist = CheckList_Get(controlId);
                checklist.Disable();        
                break;    
                
            // datepickercombos
            case 7:
                var datepicker = DatePickerCombos_Get(controlId);
                return datepicker.disable();                                                   
        }        
    },
  
    getControl : function(itemType, controlID)
    {      
        switch(itemType) {
            
            // view item / textbox / checkbox
            case 0:
            case 1:
            case 2:
                return document.getElementById(controlID);
                                        
            // combo
            case 3:
                return ComboList_Get(controlID);
                
            // selector
            case 4:
                return Selector_Get(controlID);
                                
            // datepicker
            case 5:
                return DatePicker_Get(controlID);
                
            // checklist
            case 6:
                return CheckList_Get(controlID);
                
            // datepickercombos
            case 7:
                return DatePickerCombos_Get(controlID);
        }
    },
      
    getClonedControl : function(itemType, refControlID, newControlID, value, description)
    {      
        switch(itemType) {
            
            // view item (span)
            case 0:
                var label = document.getElementById(refControlID);
                return ItemsCommon.getNewLabel(label, newControlID, value);
                
            // textbox
            case 1:
                var textbox = document.getElementById(refControlID);
                return ItemsCommon.getNewTextBox(textbox, newControlID, value);                
            
            // checkbox
            case 2:
                var checkBox = document.getElementById(refControlID);
                return ItemsCommon.getNewCheckBox(checkBox, newControlID, value);                      
                                        
            // combo
            case 3:
                var combo = ComboList_Get(refControlID);
                return ItemsCommon.getNewCombo(combo, newControlID, value);   
                
            // selector
            case 4:
                var selector = Selector_Get(refControlID);
                return ItemsCommon.getNewSelector(selector,  newControlID, value, description);                  
                                
            // datepicker
            case 5:
                var datePicker = DatePicker_Get(refControlID);
                return ItemsCommon.getNewDatePicker(datePicker, newControlID, value);   
                
            // checklist
            case 6:
                var checklist = CheckList_Get(refControlID);
                return ItemsCommon.getNewCheckList(datePicker, newControlID, value);  
                
            // datepickercombos
            case 7:
                var datePicker = DatePickerCombos_Get(refControlID);
                return ItemsCommon.getNewDatePickerCombos(datePicker, newControlID, value);   
                                
        }
    },
    
    disposeControl : function(controlID)
    {
        var control;
    
        // Combos
        if( !(typeof(ComboList_Instances)==="undefined") && (ComboList_Instances[controlID]) ){
            control = ComboList_Get(controlID);
            control.Dispose();
        }
        // Selectores
        else if( !(typeof(Selector_Instances)==="undefined") && (Selector_Instances[controlID]) ){
            control = Selector_Get(controlID);
            control.dispose();          
        }
        // DatePickers
        else if( !(typeof(DatePicker_Instances)==="undefined") && (DatePicker_Instances[controlID]) ){
            control = DatePicker_Get(controlID);
            control.dispose();               
        }          
        // CheckLists
        else if( !(typeof(CheckList_Instances)==="undefined") && (CheckList_Instances[controlID]) ){
            control = CheckList_Get(controlID);
            control.Dispose();               
        }    
        // DatePickerCombos
        else if( !(typeof(DatePickerCombos_Instances)==="undefined") && (DatePickerCombos_Instances[controlID]) ){
            control = DatePickerCombos_Get(controlID);
            control.dispose();               
        }                 
    },
    
    getNewLabel: function(label, newControlID, value)
    {
        var newLabel = document.createElement("span");
        newLabel.id = newControlID;
        newLabel.innerHTML = label.innerHTML;
        
        if(value)
            newLabel.innerHTML = value;
            
        return newLabel;
    },
    
    getNewTextBox: function(textbox, newControlID, value)
    {
        // textarea
        if(textbox.tagName == "TEXTAREA") 
        {
            return ItemsCommon.getNewTextArea(textbox, newControlID, value);
        }
        // textbox
        else
        {
            return ItemsCommon.getNewSimpleTextBox(textbox, newControlID, value);
        }    
    },
    
    getNewTextArea: function(textbox, newControlID, value)
    {
        var newTextArea = document.createElement("textarea");
        var className;
        
        if(textbox.readOnly) {
            newTextArea.setAttribute("readonly", "'readonly'");
            newTextArea.className = "textAreaDisabled";
        }
        else {
            newTextArea.className = "textArea";
        }                
        
        newTextArea.id = newControlID;
        newTextArea.rows = textbox.rows;
        newTextArea.cols = textbox.cols;
        newTextArea.className = className;
        newTextArea.style.height = textbox.style.height;
        newTextArea.style.width = textbox.style.width;
        
        Common.setEventHandler(newTextArea, "onfocusin", "this.className = 'textAreaOnFocus';");
        Common.setEventHandler(newTextArea, "onfocusout", "this.className = 'textArea';");
        Common.setEventHandler(newTextArea, "onfocus", "this.className = 'textAreaOnFocus';");
        Common.setEventHandler(newTextArea, "onblur", "this.className = 'textArea';");
        Common.setEventHandler(newTextArea, "onkeypress", "return Common.imposeMaxLength(this, 50);");
        Common.setEventHandler(newTextArea, "onkeydown", "Common.cutToMaxLength(this, 50);");
        Common.setEventHandler(newTextArea, "onkeyup", "Common.cutToMaxLength(this, 50);");

        if(value)
            newTextArea.value = value;
            
        return newTextArea;    
    },
    
    getNewSimpleTextBox: function(textbox, newControlID, value)
    {
        var newTextBox = document.createElement("input");

        if(textbox.readOnly) {
            newTextBox.setAttribute("readonly", "'readonly'");
            newTextBox.className = "textBoxDisabled";
        }
        else {
            newTextBox.className = "textBox";
        }                

        newTextBox.id = newControlID;
        newTextBox.style.width = textbox.style.width;

        if(textbox.maxLength>0)
            newTextBox.maxLength = textbox.maxLength;
        
        Common.setEventHandler(newTextBox, "onfocusin", "this.className = 'textBoxOnFocus';");
        Common.setEventHandler(newTextBox, "onfocusout", "this.className = 'textBox';");
        Common.setEventHandler(newTextBox, "onfocus", "this.className = 'textBoxOnFocus';");
        Common.setEventHandler(newTextBox, "onblur", "this.className = 'textBox';");

        if(value)
            newTextBox.value = value;
            
        return newTextBox;    
    },
    
    getNewCheckBox: function(checkBox, newControlID, value)
    {
        var newCheckBox = document.createElement("input");
        
        newCheckBox.id = newControlID;
        newCheckBox.type = "checkbox";
        newCheckBox.className = checkBox.className;
        
        if(checkBox.disabled)
            newCheckBox.setAttribute("disabled", "'disabled'");
                    
        if(value != undefined)
            newCheckBox.checked = value;
                                
        return newCheckBox; 
    },
    
    getNewCombo: function(combo, newControlID, value)
    {
        var newCombo = combo.Clone(newControlID);
        
        var div = document.createElement("div");
        div.id = "cont_" + newControlID;
        div.className = combo.cssClass;

        if(value)
            newCombo.SelectItem(value);
            
        newCombo.ShowIn(div, combo.pathimages);

        return div;
    },

    getNewSelector: function(selector, newControlID, value, description)
    {
        var newSelector = selector.clone(newControlID);
        
        var div = document.createElement("div");
        div.id = "cont_" + newControlID;
        div.className = selector.cssClass;

        if(value)
            newSelector.value = value;

        if(description)
            newSelector.description = description;
            
        newSelector.showIn(div);
        
        return div;
    },

    getNewDatePicker: function(datePicker, newControlID, value)
    {
        var newDatePicker = datePicker.clone(newControlID);
        
        var div = document.createElement("div");
        div.id = "cont_" + newControlID;
        div.className = datePicker.cssClass;

        if(value)
            newDatePicker.setValue(value);
            
        newDatePicker.showIn(div);
                    
        return div;
    },
  
    getNewCheckList: function(checklist, newControlID, value)
    {
        var newCheckList = checklist.Clone(newControlID);
        
        var div = document.createElement("div");
        div.id = "cont_" + newControlID;
        div.className = combo.cssClass;

        if(value)
            newCheckList.SetValueList(value);

        newCheckList.ShowIn(div, checklist.pathImages);
        
        return div;
    },
    
    getNewDatePickerCombos: function(datePicker, newControlID, value)
    {
        var newDatePicker = datePicker.clone(newControlID);
        
        var div = document.createElement("div");
        div.id = "cont_" + newControlID;
        div.className = datePicker.cssClass;

        if(value)
            newDatePicker.setValue(value);
            
        newDatePicker.showIn(div, datePicker.pathimages);
                    
        return div;
    }
}